<?php

/**
 * 后台菜单控制器
 * @author jackson xu <jackson.xu@wemax.com>
 * @access public
 */

namespace Admin\Controller;

use Think\Controller;

class BackMenuController extends BaseController {

    public function _initialize() {
        parent::_initialize();
    }

    //列表
    public function index() {
        $where = $this->searchConditionByName();
        $limit = $this->limit;
        $model = $this->model;
        $p = I('get.p', 1, 'intval');
        $where["pid"] = I('get.pid', 0, 'intval');
        ;
        $contents = $model->where($where)->order('sort asc,id asc')->page($p, $limit)->select();

        $count = $model->where($where)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $limit);
        $this->page = $Page->show(); // 分页显示输出
        $this->title = "列表";
        $this->contents = $contents;
        $this->display();
    }

    public function _before_add() {
        $where['pid'] = 0;
        $this->menus = D('BackMenu')->where($where)->select();
    }

    public function add() {
        if (IS_POST) {
            $data = I('post.');
            $result = D('BackMenu')->add($data);
            if ($result) {
                $this->success("新增成功", U('index'));
            } else {
                $this->error('新增错误');
            }
        }
        $this->pid = I('pid', 0);
        $this->title = '新增菜单';
        $this->display();
    }

    public function _before_edit() {
        $where['pid'] = 0;
        $this->menus = D('BackMenu')->where($where)->select();
    }

    public function sort() {
        if (IS_GET) {
            $ids = I('get.ids');
            $pid = I('get.pid');

            //获取排序的数据
            if (!empty($ids)) {
                $map['id'] = array('in', $ids);
            } else {
                if ($pid !== '') {
                    $map['pid'] = $pid;
                }
            }
            $list = D('BackMenu')->where($map)->field('id,name')->order('id asc,sort asc')->select();
            $this->assign('list', $list);
            $this->title = '菜单排序';
            $this->display();
        } elseif (IS_POST) {
            $ids = I('post.ids');
            $ids = explode(',', $ids);
            foreach ($ids as $key => $value) {
                $res = D('BackMenu')->where(array('id' => $value))->setField('sort', $key + 1);
            }
            if ($res !== false) {
                $this->success('排序成功！');
            } else {
                $this->eorror('排序失败！');
            }
        } else {
            $this->error('非法请求！');
        }
    }

    public function edit() {
        $model = $this->model;
        if (IS_POST) {
            $data = I("post.");
            $verification = $model->create();

            $result = $model->save($data);

            if ($result !== false) {
                $this->success('更新成功', U('index'));
            } else {
                $this->error('更新失败');
            }
        } else {
            $id = I('id', '', 'intval');

            if (empty($id)) {
                $this->error('参数不能为空！');
            }

            /* 获取一条记录的详细数据 */
            $data = $this->model->detail($id);
            if (!$data) {
                $this->error($this->model->getError());
            }
            $this->assign('info', $data);
            $this->title = '编辑';
            $this->display();
        }
    }

}
